home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / WASTE 1.2a4 / WASTE Demo / LongControls.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-05  |  5.3 KB  |  258 lines  |  [TEXT/CWIE]

  1. /*
  2.     WASTE Demo Project:
  3.     Macintosh Controls with Long Values
  4.     
  5.     Copyright © 1993-1995 Marco Piovanelli
  6.     All Rights Reserved
  7.  
  8.     C port by John C. Daub
  9. */
  10.  
  11. #ifndef __CONTROLS__
  12. #include <Controls.h>
  13. #endif
  14. #ifndef __FIXMATH__
  15. #include <FixMath.h>
  16. #endif
  17. #ifndef __TOOLUTILS__
  18. #include <ToolUtils.h>
  19. #endif
  20.  
  21. #ifndef __WEDEMOAPP__
  22. #include "WEDemoIntf.h"
  23. #endif
  24.  
  25.  
  26. // long control auxiliary record used for keeping long settings
  27. // a handle to this record is stored in the reference field of the control record
  28.  
  29. struct    LCAuxRec
  30. {
  31.     long    value;    // long value
  32.     long    min;    // long min
  33.     long    max;    // long max
  34. };
  35. typedef struct LCAuxRec LCAuxRec, *LCAuxPtr, **LCAuxHandle;
  36.  
  37.  
  38.  
  39. OSErr    LCAttach( ControlRef control )
  40. {
  41.     Handle        aux;
  42.     LCAuxPtr    pAux;
  43.     OSErr        reply = noErr;
  44.     
  45.     /*    allocate the auxiliary record that will hold long settings */
  46.     
  47.     aux = NewHandleClear( sizeof( LCAuxRec ) );
  48.     if ( aux == NULL )
  49.     {
  50.         reply = MemError();
  51.         return    reply;
  52.     }
  53.     
  54.     /*    store a handle to the auxiliary record in the contrlRfCon field */
  55.     
  56.     SetControlReference( control, (long)aux );
  57.         
  58.     /*    copy current control settings into the auxiliary record */
  59.     
  60.     pAux = *((LCAuxHandle)aux);
  61.     pAux->value = GetControlValue( control );
  62.     pAux->min = GetControlMinimum( control );
  63.     pAux->max = GetControlMaximum( control );
  64.     
  65.     return reply;
  66. }
  67.  
  68.  
  69.  
  70. void    LCDetach( ControlRef control )
  71. {
  72.     Handle            aux;
  73.     
  74.     aux = (Handle)GetControlReference( control );
  75.         
  76.     if ( aux != NULL )
  77.     {
  78.         SetControlReference( control, 0 );        
  79.         DisposeHandle( aux );
  80.     }
  81.     
  82.     return;
  83. }
  84.  
  85.  
  86.  
  87. void    LCSetValue( ControlRef control, long value )
  88. {
  89.     LCAuxPtr        pAux;
  90.     short            controlMin, controlMax, newControlValue;
  91.         
  92.     pAux = *((LCAuxHandle)GetControlReference( control ));
  93.  
  94.     /*    make sure value is in the range min...max */
  95.     
  96.     if ( value < pAux->min )
  97.         value = pAux->min;
  98.     if ( value > pAux->max )
  99.         value = pAux->max;
  100.     
  101.     /*    save value in auxiliary record */
  102.     
  103.     pAux->value = value;
  104.     
  105.     /*    calculate new thumb position */
  106.     
  107.     controlMin = GetControlMinimum( control );
  108.     controlMax = GetControlMaximum( control );
  109.     newControlValue = controlMin + FixRound( FixMul ( FixDiv( value - pAux->min, 
  110.                 pAux->max - pAux->min), BSL(controlMax - controlMin, 16 )));
  111.     
  112.     /*    do nothing if the thumb position hasn't changed */
  113.     
  114.     if ( newControlValue != GetControlValue(control) )
  115.         SetControlValue( control, newControlValue );
  116.  
  117.     return;
  118. }
  119.  
  120. void    LCSetMin( ControlRef control, long min )
  121. {
  122.     LCAuxPtr        pAux;
  123.     
  124.     pAux = *((LCAuxHandle)GetControlReference( control ));
  125.     
  126.     /*    make sure min is less than or equal to max */
  127.     
  128.     if ( min > pAux->max )
  129.         min = pAux->max;
  130.     
  131.     /*    save min in auxiliary record */
  132.     pAux->min = min;
  133.     
  134.     /*    set contrlMin field to min or SHRT_MIN, whichever is greater */
  135.     
  136.     if ( min < SHRT_MIN )
  137.         min = SHRT_MIN;
  138.     
  139.     SetControlMinimum( control, min );
  140.     
  141.     /*    reset value */
  142.     
  143.     LCSetValue( control, pAux->value );
  144.     
  145.     return;
  146. }
  147.  
  148. void    LCSetMax( ControlRef control, long max )
  149. {
  150.     LCAuxPtr        pAux;
  151.     
  152.     pAux = *((LCAuxHandle)GetControlReference( control ));
  153.  
  154.     /*    make sure max is greater than or equal to min */
  155.     
  156.     if ( max < pAux->min )
  157.         max = pAux->min;
  158.     
  159.     /*    save max in auxiliary record */
  160.     
  161.     pAux->max = max;
  162.     
  163.     /*    set contrlMax field to max or SHRT_MAX, whichever is less */
  164.     
  165.     if ( max > SHRT_MAX )
  166.         max = SHRT_MAX;
  167.     
  168.     SetControlMaximum( control, max );
  169.     
  170.     /*    reset value */
  171.     
  172.     LCSetValue( control, pAux->value );
  173.     
  174.     return;
  175. }
  176.  
  177. /*    In each of these LCGetXXX() functions, there are 2 ways listed to do things.  They are
  178.     both the same thing and perform the same stuff, just one is easier to read than the
  179.     other (IMHO).  I asked Marco about it and he gave me the shorter code (what's commented
  180.     in each function) and gave me this explanation:
  181.     
  182.         This version [the commented code] yields smaller and faster code
  183.         (try disassembling both versions if you wish), but some people may
  184.         find it somewhat harder to read.
  185.     
  186.     I agree with Marco that his code is better overall, but in the interest of readabilty
  187.     (since this demo is a learning tool), I left my code in and put Marco's in commented
  188.     out.  Pick whichever you'd like to use.
  189. */
  190.  
  191.  
  192. long    LCGetValue( ControlRef control )
  193. {
  194.     LCAuxPtr    pAux;
  195.     
  196.     pAux = *((LCAuxHandle)GetControlReference( control ));
  197.  
  198.     return pAux->value;
  199.  
  200. //    this is Marco's code.  Remember, this is a little harder to read, but overall
  201. //    yields tighter code.
  202.  
  203. //    return (* (LCAuxHandle) GetControlReference(control)) -> value;
  204.  
  205. }
  206.  
  207.  
  208. long    LCGetMin( ControlRef control )
  209. {
  210.     LCAuxPtr    pAux;
  211.     
  212.     pAux = *((LCAuxHandle)GetControlReference( control ));
  213.     
  214.     return pAux->min;
  215.  
  216. //    this is Marco's code.  Remember, this is a little harder to read, but overall
  217. //    yields tighter code.
  218.  
  219. //    return (* (LCAuxHandle)GetControlReference(control)) -> min;
  220.     
  221. }
  222.  
  223.  
  224. long    LCGetMax( ControlRef control )
  225. {
  226.     LCAuxPtr    pAux;
  227.     
  228.     pAux = *((LCAuxHandle)GetControlReference( control ));
  229.     
  230.     return pAux->max;
  231.  
  232. //    this is Marco's code.  Remember, this is a little harder to read, but overall
  233. //    yields tighter code.
  234.  
  235. //    return (* (LCAuxHandle)GetControlReference(control)) -> max;
  236.  
  237. }
  238.  
  239.  
  240. void    LCSynch( ControlRef control )
  241. {
  242.     LCAuxPtr        pAux;
  243.     short            controlMin, controlMax, controlValue;
  244.     
  245.     controlMin = GetControlMinimum( control );
  246.     controlMax = GetControlMaximum( control );
  247.     controlValue = GetControlValue( control );    
  248.     pAux = *((LCAuxHandle)GetControlReference( control ));
  249.     
  250.     /*    calculate new long value */
  251.     
  252.     pAux->value = pAux->min + FixMul( FixRatio ( controlValue - controlMin,
  253.                   controlMax - controlMin), pAux->max - pAux->min );
  254.     
  255.     return;
  256. }
  257.  
  258.